package com.atguigu.gmall.service.minioservice.Impl;

import com.atguigu.gmall.service.minio.MinioProperties;
import com.atguigu.gmall.service.minioservice.OSSService;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.InputStream;

import java.util.UUID;
@Service
public class OSSServiceImpl implements OSSService {
    @Autowired
    MinioClient minioClient;
    @Autowired
    MinioProperties minioProperties;
    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        //文件名
        String filename =UUID.randomUUID().toString().replace("-", "")+"_"+file.getOriginalFilename();
        //文件流
        InputStream inputStream = file.getInputStream();
        //Minio上传需要的配置项
        PutObjectOptions putObjectOptions = new PutObjectOptions(inputStream.available(), -1);
        //文件类型
        String contentType = file.getContentType();
        putObjectOptions.setContentType(contentType);
        //上传
        minioClient.putObject(minioProperties.getBucket(), filename, inputStream, putObjectOptions);
        //返回给前端的路径
        String url=minioProperties.getEndpoint()+"/"+minioProperties.getBucket()+"/"+filename;
        return url;
    }
}
